var xmlhttp;

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

// change ranking type
function loadRank(uid){
	var sort = document.getElementById("ranking_select").value;
	var query = "sort="+sort;
	
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4 && xmlhttp.status==200){
    		$("table#ranking_table").replaceWith(construct_rank_table(uid));
		}
	}
	
	xmlhttp.open("GET","rank_table.php?"+query,true);
	xmlhttp.send();
}

//function to construct a ranking table according to the result of query
function construct_rank_table(uid){
	var xmldoc,x,xx,i,txt,self;
	txt = "<table id=\"ranking_table\">";
	txt = txt + "<tr><th width=\"3%\"></th>" +
			"<th width=\"15%\">Name</th>" +
			"<th width=\"5%\">Level</th>" +
			"<th width=\"10%\">Gold</th>" +
			"<th width=\"10%\">Exp</th>" +
			"<th width=\"8%\">Question</th>" +
			"<th width=\"8%\">BugKill</th></tr>";
	xmldoc = xmlhttp.responseXML;

	// construct a table
	x = xmldoc.getElementsByTagName("rank");
	for(i=0;i<x.length;i++){
		xx = x[i].getElementsByTagName("uid");
		if(xx[0].firstChild.nodeValue == uid){
			self = "<td class=\"self_user\">";
		}else{
			self = "<td>";
		}
		txt = txt + "<tr>" + self +(i+1)+"</td>" + self;
		xx = x[i].getElementsByTagName("name");
		txt = txt + xx[0].firstChild.nodeValue;
		txt = txt + "</td>" + self;
		xx = x[i].getElementsByTagName("level");
		txt = txt + xx[0].firstChild.nodeValue;
		txt = txt + "</td>" + self;
		xx = x[i].getElementsByTagName("gold");
		txt = txt + xx[0].firstChild.nodeValue;
		txt = txt + "</td>" + self;
		xx = x[i].getElementsByTagName("exp");
		txt = txt + xx[0].firstChild.nodeValue;
		txt = txt + "</td>" + self;
		xx = x[i].getElementsByTagName("qcreated");
		txt = txt + xx[0].firstChild.nodeValue;
		txt = txt + "</td>" + self;
		xx = x[i].getElementsByTagName("bugkills");
		txt = txt + xx[0].firstChild.nodeValue;
		txt = txt + "</td></tr>";
	}
	
	txt = txt + "</table>";
	//alert(txt); // test
	return txt;
}